<?php
	header('Content-Type: application/json');

	include_once('../../connect2db.php');
	include_once('../exception.php');
	include_once('../query_list.php');
	ini_set("display_errors", "stderr");

	$data_file = 'data.json';
	$return_object = array();
	$exception = new API_Exception();

	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		switch ($_REQUEST['v1']) {
			case 'bigiot_receiving':
				$post = json_decode(file_get_contents('php://input'), true);

				# file_put_contents($data_file, json_encode($post) . PHP_EOL, FILE_APPEND | LOCK_EX);

				$return_object = bigiot_receiving(
					@$post['client_id'],
					@$post['topic'],
					@$post['username'],
					@$post['payload'],
					@$post['peername'],
					@$post['timestamp']
				);

				break;
			default:
				$return_object = $exception->get_response_object(1001);
		}
	} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
		switch ($_REQUEST['v1']) {
			case 'bigiot_echarts':
				$return_object = bigiot_echarts();

				break;
			case 'bigiot_echarts_count':
				$return_object = bigiot_echarts_count();

				break;
			default:
				$return_object = $exception->get_response_object(1001);	
		}
	} else {
		$return_object = $exception->get_response_object(1001);
	}

	echo json_encode($return_object);

	function bigiot_echarts_count() {
		global $exception, $mysqli;

		$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_echarts_count);
		$stmt->execute();
		$stmt->store_result();

		$return_object = (object) array(
			"result" => "success",
			"counter" => $stmt->num_rows
		);

		$stmt->close();
		$mysqli->close();

		return $return_object;
	}

	function bigiot_echarts() {
		global $exception, $mysqli;

		$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_echarts_title);
		$stmt->execute();
		$stmt->store_result();
		$stmt->bind_result($title);

		$titles = array();
		while ($stmt->fetch()) {
			$titles[] = $title;
		}

		$options = array();
		foreach ($titles as $title) {
			$series_array = array();
			
			$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_echarts_series_data);
			$stmt->bind_param("s", $title);
			$stmt->execute();
			$stmt->store_result();
			$stmt->bind_result($payload_value, $timestamp);

			$series_list = array();
			$xAxis_list = array();
			while ($stmt->fetch()) {
				$series_list[] = $payload_value;
				$xAxis_list[] = $timestamp;
			}

			// $list[] = $client_id
			$series_array[] = array(
				// "name" => $title,
				"type" => "line",
				"smooth" => true,
				"data" => $series_list,
				"markPoint" => array(
					"data" => array(
						array(
							"type" => "max",
							"name" => "max"
						),
						array(
							"type" => "min",
							"name" => "min"
						)
					)
				)
			);

			$options[] = array(
				"title" => $title,
				"xAxis" => $xAxis_list,
				"series" => $series_array
			);
		}

		$return_object = (object) array(
			"result" => "success",
			"options" => $options
		);

		$stmt->close();
		$mysqli->close();

		return $return_object;
	}

	function bigiot_receiving($client_id, $topic, $username, $payload, $peername, $timestamp) {
		global $exception, $mysqli;

		// 验证参数合法性
		if (!$client_id) {return $exception->get_response_object(2000);}
		if (!$topic) {return $exception->get_response_object(2001);}
		if (!$username) {return $exception->get_response_object(2002);}
		if (!$payload) {return $exception->get_response_object(2003);}
		if (!$peername) {return $exception->get_response_object(2004);}
		if (!$timestamp) {return $exception->get_response_object(2005);}

		$payload = json_decode($payload, true);

		if (!$payload) {return $exception->get_response_object(2006);}

		$payload_key = $payload['key'];
		$payload_value = $payload['vlue'];

		// 插入记录
		$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_append_record);
		$stmt->bind_param("ssssssi", $client_id, $topic, $username, $payload_key, $payload_value, $peername, $timestamp);
		$stmt->execute();
		$stmt->close();

		$return_object = (object) array(
			"result" => "success"
		);

		clean_outdate_data($timestamp);

		return $return_object;
	}
	
	function clean_outdate_data($timestamp) {
		global $mysqli;

		$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_first_recorder);
		$stmt->execute();
		$stmt->store_result();
		$stmt->bind_result($first_timestamp);
		$stmt->fetch();
		$stmt->close();

		if ((round($timestamp / 1000) - $first_timestamp) > (4 * 24 * 60 * 60)) {
			$deadline = (round($timestamp / 1000) - 24 * 60 * 60) * 1000;

			$stmt = $mysqli->prepare(query_list_bigiot_xyz::query_clean_outdate_data);
			$stmt->bind_param("i", $deadline);
			$stmt->execute();
			$stmt->close();
		}

		$mysqli->close();
	}
?>